package controllers.listeners.lexicon;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JOptionPane;

import models.LexiconModel;
import models.LexiconFileModel;
import views.ViewLexicon;

/* Opens the chosen lexicon file (e.g. "Tagalog", "Chinese", etc.) */
/* actionPerformed when you click the "Load lexicon file" button */
public class OpenLexiconListener implements ActionListener{
	
	ViewLexicon view;
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		File file = null;
		
		LexiconFileModel.setToSave(false);
		LexiconFileModel.readNewFile();
		file = LexiconFileModel.getFile();
		
		if (file == null) {
			return;
		}
		
		try {
			LexiconModel.read();
		}
		catch (Exception ex) {
			JOptionPane.showMessageDialog(null, "The file could not be opened. Please try again.", "Error", JOptionPane.ERROR_MESSAGE);
		}
		
		view.setLexicon(LexiconModel.getLexicon());
		view.refreshCmbSyntacticCategory();
		view.refreshTable();
	}
		
	public void setView(ViewLexicon view){
		this.view = view;
	}
}
